#include <func.h>
int Compute_file_md5(int fd,char *md5_str);
typedef struct train_s {
    int length;
    char data[1000];
} train_t;

int recvn(int netfd, void *buf, int length){
    int total = 0;
    char *p = (char *)buf;
    while(total < length){
        int ret = recv(netfd,p+total,length-total,0);
        if(ret == 0){
            return -1;
        }
        total += ret;
    }
    return 0;
}

int sendFile(int netfd,char *filename){
    int fd=open(filename,O_RDWR,0666);
    ERROR_CHECK(fd,-1,"open");
    printf ("i can open\n");
    train_t train;
    bzero(&train,sizeof(train));

    struct stat statbuf;
    fstat(fd,&statbuf);
    train.length=sizeof(statbuf.st_size);
    memcpy(train.data,&statbuf.st_size,train.length);
    

    printf("train.length=%d  trian.data=%s\n",train.length,train.data);
    send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
    off_t cursize =0;
    off_t lastsize = 0;

    while(1){
        bzero(&train,sizeof(train));
        ssize_t ret = read(fd,train.data,sizeof(train.data));
        if(ret==0){
            break;
        }
        train.length=ret;
        send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
        cursize += ret;
        if(cursize-lastsize >0){
            //printf("%5.2lf%%\r",100.0*cursize / statbuf.st_size);
            //fflush(stdout);
            lastsize = cursize;
        }
    }
    train.length=0;
    send(netfd,&train.length,sizeof(train.length),0);
    printf("100%%\n");
    close(fd);
    printf("上传完成\n");
    return 0;
}

int getsfile(int netfd,char *filename){
    ssize_t fileLength=0;
    time_t start_time;
    time_t end_time;
    
    int fd=open(filename,O_RDWR,0666);
    if(fd==-1){
        fd=open(filename,O_RDWR|O_CREAT|O_TRUNC,0666);
    }    
    ERROR_CHECK(fd,-1,"open");

    struct stat statbuf;
    fstat (fd,&statbuf);
    fileLength =  statbuf.st_size;

    if(fileLength!=0){
        lseek(fd,fileLength,SEEK_SET);
    }

    send(netfd,&fileLength,sizeof(ssize_t),MSG_NOSIGNAL);
    int length;
    off_t filesize=0;
    
    
    recvn(netfd,&filesize,sizeof(filesize));
    printf("文件将从%5.2lf%%开始下载\n",100.0*fileLength/(filesize+fileLength));
    start_time= time(NULL);
    off_t cursize = fileLength;
    off_t slice = 0;
    off_t lastsize = fileLength; 
    char buf[1024] = {0};
    
    while(1){
        length =0;
        recvn(netfd,&length,sizeof(int));
        
        if(length == 0){
            break;
        }
        bzero(buf,sizeof(buf));
        recvn(netfd,buf,length);
        cursize += length;
        
        //while(1){
        //    sleep(1);
        //    printf("asdasdasdasd\n");
        //}
        if(cursize - lastsize > slice){
            //printf("%5.2lf%%\r",100.0 * cursize / (filesize+fileLength));
            //fflush(stdout);
            lastsize = cursize;
        }
        write(fd,buf,length);
    }
    printf("100.00%%\n");
    printf("下载完成\n");
    end_time = time(NULL);
    printf("下载时间为%ld秒\n",end_time-start_time);
    close(fd);
    return 0;
}
